<template>
  <el-card>
    <el-row>
      <el-form :model="state" :inline="true">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="state.roleName" placeholder="请输入角色名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="角色代码" prop="roleCode">
          <el-input v-model="state.roleCode" placeholder="请输入角色代码" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getRoleListFun">查询</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row class="searchDiv">
      <el-button v-if="hasPer('role:add')" type="primary" icon="Plus" @click="editRoleFun">新增</el-button>
    </el-row>
    <el-table :data="state.tableData" row-key="id" :border="true">
      <el-table-column label="序号" type="index" width="60" align="center"></el-table-column>
      <el-table-column label="角色名称" prop="roleName" align="center"></el-table-column>
      <el-table-column label="角色代码" prop="roleCode" align="center"></el-table-column>
      <el-table-column label="角色说明" prop="description" align="center"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" prop="option" width="240px" align="center">
        <template #default="scope">
          <el-button v-if="hasPer('role:authorize')" type="success" @click="authorizeRoleFun(scope.row.roleId)">授权</el-button>
          <el-button v-if="hasPer('role:edit')" type="primary" @click="editRoleFun(JSON.parse(JSON.stringify(scope.row)))">编辑</el-button>
          <el-button v-if="hasPer('role:del')" type="danger" @click="delRoleFun(scope.row.roleId, scope.row.roleName)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <pagination v-model:current="state.current" v-model:size="state.size" v-model:total="state.total" @get-list="getRoleListFun"></pagination>
  </el-card>
  <!--编辑-->
  <edit-role v-model:dialog-visible="dialogVisible" :role-obj="state.roleObj" @get-list="getRoleListFun"></edit-role>
  <!--授权-->
  <authorize-role v-model:auth-visible="authVisible" :role-id="state.roleId"></authorize-role>
</template>

<script setup>
import editRole from "./editRole";
import authorizeRole from "./authorizeRole";
import Pagination from "../../components/Pagination";
import {getRoleList, delRole} from "../../api/role/sysRole";
import {errorMsg, infoMsg, successMsg} from "../../utils/message";
import {hasPer, parseTime} from "../../utils/common";
import {onMounted, reactive, ref} from "vue";
import {ElMessageBox} from "element-plus";

const state = reactive({
  roleCode: '',
  roleName: '',
  tableData: [],
  roleId: null,
  roleObj: {},
  current: 1,
  size: 10,
  total: 0
})

const dialogVisible = ref(false)

const authVisible = ref(false)

onMounted(() => {
  getRoleListFun()
})

//  获取角色列表
const getRoleListFun = () => {
  const params = {
    roleCode: state.roleCode,
    roleName: state.roleName,
    size: state.size,
    currentPage: state.current
  }
  getRoleList(params).then(res => {
    if (res.code == 200){
      state.tableData = res.data.records
      state.total = res.data.total
    } else {
      errorMsg(res.message)
    }
  })
}
//  编辑角色
const editRoleFun = (row) => {
  dialogVisible.value = true
  state.roleObj = row.roleId ? row : {}
}
//  角色授权
const authorizeRoleFun = (roleId) => {
  authVisible.value = true
  state.roleId = roleId
}
//  删除角色
const delRoleFun = (roleId, name) => {
  ElMessageBox.confirm('确定删除角色【'+name+'】？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    delRole({roleId: roleId}).then(res => {
      if (res.code == 200){
        successMsg(res.message)
        getRoleListFun()
      } else {
        errorMsg(res.message)
      }
    })
  }).catch(() => {
    infoMsg('操作已取消')
  })
}
</script>

<style scoped>

</style>